home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / SnoozeAlarm / BetterBitStream.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.8 KB  |  83 lines

  1. /****************************************************************************************
  2.     BetterBitStream.c
  3.     
  4.     Copyright © 1999 Red Shed Software. All rights reserved.
  5.     by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
  6.     
  7.     Commenter    Date                Comment
  8.     ---------    -----------------    -----------------------------------------------------
  9.     wolf        Thu, Mar 18, 1999    Created.
  10.     
  11.     ************************************************************************************/
  12.  
  13. #include    "BetterBitStream.h"
  14.  
  15. /****************************************************************************************
  16.     Commenter    Date                Comment
  17.     ---------    -----------------    -----------------------------------------------------
  18.     wolf        Tue, Mar 16, 1999    Created.
  19.     
  20.     ************************************************************************************/
  21.  
  22.     void
  23. InsertBetterBitStream(
  24.     void    **bitStream,    //    ->    A pointer to a pointer to a buffer.
  25.                             //    <-    Incremented with each filled byte.
  26.     UInt32    *bitPosition,    //    ->    Where to put the bit.
  27.                             //    <-    Incremented.
  28.     UInt8    bit )            //    ->    What value to insert.
  29. {
  30.     typedef    struct    {    UInt8    set, clear; } Operation;
  31.     static    Operation    opTable[ 8 ]    =    {    1 << 7, ~(1 << 7), 
  32.                                                 1 << 6, ~(1 << 6), 
  33.                                                 1 << 5, ~(1 << 5), 
  34.                                                 1 << 4, ~(1 << 4), 
  35.                                                 1 << 3, ~(1 << 3), 
  36.                                                 1 << 2, ~(1 << 2), 
  37.                                                 1 << 1, ~(1 << 1), 
  38.                                                 1 << 0, ~(1 << 0) };
  39.     UInt8        *byteStream = (UInt8*) *bitStream;
  40.     UInt8        bitPositionLow3Bits = (*bitPosition & 0x00000007);
  41.     Operation    *operation = &opTable[ bitPositionLow3Bits ];
  42.     
  43.     if( bit == 0x00 )
  44.         *byteStream &= operation->clear;
  45.     else
  46.         *byteStream |= operation->set;
  47.     if( bitPositionLow3Bits == 7 )    //    This byte is full, increment it.
  48.         *bitStream = ++byteStream;
  49.     ++(*bitPosition);
  50. }
  51.  
  52. /****************************************************************************************
  53.     Commenter    Date                Comment
  54.     ---------    -----------------    -----------------------------------------------------
  55.     wolf        Thu, Mar 18, 1999    Created.
  56.     
  57.     ************************************************************************************/
  58.  
  59.     UInt8                    //    <-    The retrieved bit.
  60. GetBetterBitStream(
  61.     void    **bitStream,    //    ->    A pointer to a pointer to a buffer.
  62.                             //    <-    When a byte is emptied, the pointer is
  63.                             //        automatically incremented.
  64.     UInt32    *bitPosition )    //    ->    Current position in the buffer.
  65.                             //    <-    Automatically incremented.
  66. {
  67.     static    UInt8    opTable[ 8 ]    =    {    1 << 7,
  68.                                             1 << 6,
  69.                                             1 << 5,
  70.                                             1 << 4,
  71.                                             1 << 3,
  72.                                             1 << 2,
  73.                                             1 << 1,
  74.                                             1 << 0 };
  75.     UInt8        *byteStream = (UInt8*) *bitStream;
  76.     UInt8        bitPositionLow3Bits = (*bitPosition & 0x00000007);
  77.     UInt8        bit = (*byteStream) & opTable[ bitPositionLow3Bits ];
  78.     
  79.     if( bitPositionLow3Bits == 7 )    //    This byte is empty, increment it.
  80.         *bitStream = ++byteStream;
  81.     ++(*bitPosition);
  82.     return( bit == 0x00 ? 0x00 : 0x01 );
  83. }